﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class warehouseGoodsInventory : BaseAdmin
    {
        readonly DALWarehouseGoods theWarehouseGoods = new DALWarehouseGoods();
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        readonly DALWarehouseGoodsInventory theWarehouseGoodsInventory = new DALWarehouseGoodsInventory();
        readonly DALWarehouseGoodsInventoryDetail theWarehouseGoodsInventoryDetail = new DALWarehouseGoodsInventoryDetail();
        tbWarehouseGoodsInventory oWarehouseGoodsInventory;
        tbWarehouseGoodsInventoryDetail oWarehouseGoodsInventoryDetail;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindWarehouse();
                this.DeleteItem();
                this.BuildForm();
              
            }

        }

        void DeleteItem()
        {
            if (Request.QueryString["ot"] == "del" && Request.QueryString["inventoryid"] != null)
            {
                if (theWarehouseGoodsInventoryDetail.Delete(int.Parse(Request.QueryString["inventoryid"])))
                {
                    ExtAspNet.Alert.Show("删除成功！");
                }
            }
        }

        private void BindGridDetail()
        {


            DataTable table;
            if (Request.QueryString["ot"] == "ad")
            {
                if (this.ddlWarehouse.SelectedItem.Value == "-1")
                {
                    table = null;
                }
                else
                {
                    table = theWarehouseGoodsInventoryDetail.GetNoInventoryGoodsList(int.Parse(this.ddlWarehouse.SelectedItem.Value)).Tables[0];
                }
            }
            else
            {
                table = theWarehouseGoodsInventoryDetail.GetList(" inventoryId=" + Request.QueryString["id"]).Tables[0];
            }
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }
        protected string InventoryId
        {
            get
            {
                return Request.QueryString["id"];
            }
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table;
            if (Request.QueryString["ot"] == "ad")
            {
                table = theWarehouseGoodsInventoryDetail.GetNoInventoryGoodsList(int.Parse(this.ddlWarehouse.SelectedItem.Value)).Tables[0];
            }
            else
            {
                table = theWarehouseGoodsInventoryDetail.GetList(" inventoryId=" + Request.QueryString["id"]).Tables[0];
            }
            Grid1.DataSource = table;
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        void BuildForm()
        {
            if (Request.QueryString["id"] != null)
            {
                oWarehouseGoodsInventory = theWarehouseGoodsInventory.GetModel(int.Parse(Request.QueryString["id"]));
                this.txtTheme.Text = oWarehouseGoodsInventory.theme;
                this.dtpStartTime.Text = oWarehouseGoodsInventory.inventoryTime.ToString("yyyy-MM-dd");
                this.txtRemark.Text = oWarehouseGoodsInventory.inventoryRemark;
                if (this.ddlWarehouse.Items.FindByValue(oWarehouseGoodsInventory.warehouseId.ToString()) != null)
                {
                    this.ddlWarehouse.Items.FindByValue(oWarehouseGoodsInventory.warehouseId.ToString()).Selected = true;
                }
                this.BindGridDetail();
            }
            if (Request.QueryString["ot"] == "view")
            {
                this.txtRemark.Readonly = true;
                this.dtpStartTime.Readonly = true;
                this.txtTheme.Readonly = true;
                this.ddlWarehouse.Enabled = false;
                this.Grid1.Columns[9].Hidden = true;
                this.Grid1.Columns[10].Hidden = true;
                this.Grid1.EnableCheckBoxSelect = false;
            }

            if (Request.QueryString["ot"] == "ad")
            {
                this.Grid1.Columns[9].Hidden = true;
                this.Grid1.Columns[10].Hidden = true;
                this.btnSubmitForm1.Hidden = false;
            }
            else
            {
                this.btnSubmitForm1.Hidden = true;
                this.Grid1.EnableCheckBoxSelect = false;
            }
        }

        void BindWarehouse()
        {
            DataTable dt = theWarehouse.GetList("").Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", "-1"));

        }

        public string HeaderHtml
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"] != "0")
                {

                    oWarehouseGoodsInventory = theWarehouseGoodsInventory.GetModel(int.Parse(Request.QueryString["id"]));

                    string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                              + "盘点管理-> 盘点编辑:{0} "
                                              + " <a style=\" display:block; float:right;\" href=\"javascript:location.href='warehouseGoodsInventoryList.aspx';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                              + " </div>"
                                             , oWarehouseGoodsInventory.theme);
                    return sHead;
                }
                else
                {
                  

                    string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                              + "盘点管理->添加盘点 "
                                              + " <a style=\" display:block; float:right;\" href=\"javascript:location.href='warehouseGoodsInventoryList.aspx';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                              + " </div>");
                    return sHead;
                }
               

            }


        }


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            oWarehouseGoodsInventory = new tbWarehouseGoodsInventory();
            oWarehouseGoodsInventory.inventoryAdminId = int.Parse(base.AdminUserId);
            oWarehouseGoodsInventory.inventoryRemark = this.txtRemark.Text.Trim();
            oWarehouseGoodsInventory.inventoryStates = 0;
            oWarehouseGoodsInventory.inventoryTime = DateTime.Parse(this.dtpStartTime.Text);
            oWarehouseGoodsInventory.theme = this.txtTheme.Text.Trim();
            oWarehouseGoodsInventory.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
            oWarehouseGoodsInventory.id = theWarehouseGoodsInventory.Add(oWarehouseGoodsInventory);

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];

                oWarehouseGoodsInventoryDetail = new tbWarehouseGoodsInventoryDetail();
                oWarehouseGoodsInventoryDetail.costPrice = decimal.Parse(this.Grid1.Rows[rowIndex].Values[5].ToString());
                oWarehouseGoodsInventoryDetail.differenceCount = 0;
                oWarehouseGoodsInventoryDetail.goodsBatchNo = this.Grid1.Rows[rowIndex].Values[2].ToString();
                oWarehouseGoodsInventoryDetail.goodsId = int.Parse(this.Grid1.DataKeys[rowIndex][1].ToString());
                oWarehouseGoodsInventoryDetail.goodsName = this.Grid1.Rows[rowIndex].Values[3].ToString();
                oWarehouseGoodsInventoryDetail.goodsNo = this.Grid1.Rows[rowIndex].Values[1].ToString();
                oWarehouseGoodsInventoryDetail.goodsSpe = "";
                oWarehouseGoodsInventoryDetail.inventoryId = oWarehouseGoodsInventory.id;
                oWarehouseGoodsInventoryDetail.paperCount = int.Parse(this.Grid1.Rows[rowIndex].Values[6].ToString());
                oWarehouseGoodsInventoryDetail.realyCount = int.Parse(this.Grid1.Rows[rowIndex].Values[7].ToString());
                oWarehouseGoodsInventoryDetail.ModelTypeId = int.Parse(this.Grid1.DataKeys[rowIndex][2].ToString());
                theWarehouseGoodsInventoryDetail.Add(oWarehouseGoodsInventoryDetail);
            }
            ExtAspNet.Alert.Show("添加成功", "", "window.location.href='warehouseGoodsInventoryList.aspx';");

        }

        protected void ddlWarehouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlWarehouse.SelectedItem.Value != "-1")
            {
                this.BindGridDetail();
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGridDetail();
        }

    }
}